package com.jlcindia.hibernate;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.jlcindia.util.HibernateUtil;

public class Lab46 {
	public static void main(String args[]) {
		Transaction tx = null;
		try {
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			tx = session.beginTransaction();

			System.out.println("1.All the contacts");
			List<Contact> clist1 = session.getNamedQuery("JLCAllContacts").list();
			for (Contact c : clist1) {
				System.out.println(c.getContactId() + "\t" + c.getFirstName()
						+ "\t" + c.getLastName() + "\t" + c.getEmail() + "\t"
						+ c.getPhone() + "\t" + c.getStatus());
			}

			System.out.println("2. All Active contacts");
			List<Contact> clist2 = session.getNamedQuery("JLCActiveContacts").setString("st", "Active").list();
			for (Contact c : clist2) {
				System.out.println(c.getContactId() + "\t" + c.getFirstName()
						+ "\t" + c.getLastName() + "\t" + c.getEmail() + "\t"
						+ c.getPhone() + "\t" + c.getStatus());
			}

			System.out.println("3. Email of All the Contacts");
			List<String> elist = session.getNamedQuery("EmailsOfAllContacts").list();
			for (String str : elist) {
				System.out.println(str);
			}
			
			System.out.println("4. Email,phones and status of All the Contacts");
			List<Object []> eplist = session.getNamedQuery("EmailsAndPhonesOfAllContacts").list();
			for (Object[] obj : eplist) {
				for (Object o : obj) {
					System.out.println(o + "\t");
				}
				System.out.println(" ");
			}

			tx.commit();
			session.close();

		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		}
	}
}
